package com.event.controller;


import com.event.common.Result;
import com.event.common.ResultCodeEnum;
import com.event.pojo.StudySrc;
import com.event.service.StudySrcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 学习资料模块 前端控制器
 * </p>
 *
 * @author 孤雏
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/studySrc")
@CrossOrigin(origins = "*", maxAge = 3600)
public class StudySrcController {
    @Autowired
    StudySrcService studySrcService;

    @GetMapping("/selectStudySrc")
    public Result<List<StudySrc>> selectStudySrc(StudySrc studySrc) {
        List<StudySrc> allSrc = studySrcService.selectStudySrc(studySrc);
        return allSrc.size()==0 ? Result.error(ResultCodeEnum.NOTFOUNDERROR) : Result.ok_data(allSrc, ResultCodeEnum.SUCCESS, allSrc.size());
    }

    @GetMapping("/selectStudySrcByName/{srcName}")
    public Result<List<StudySrc>> selectStudySrcByName(@PathVariable("srcName") String srcName) {
        List<StudySrc> srcList = studySrcService.selectStudySrcByName(srcName);
        return srcList.size()==0 ? Result.error(ResultCodeEnum.NOTFOUNDERROR) : Result.ok_data(srcList, ResultCodeEnum.SUCCESS, srcList.size());
    }

    @PostMapping("/deleteStudySrc/{srcId}")
    public Result<Void> deleteStudySrc(@PathVariable Long srcId) {
        boolean b = studySrcService.deleteStudySrc(srcId);
        if(!b) return Result.error(ResultCodeEnum.DELETEERROR);
        return Result.ok();
    }

    @PostMapping("/insertStudySrc")
    public Result insertStudySrc(@RequestBody StudySrc studySrc) {
        int i = studySrcService.insertStudySrc(studySrc);
        if(i>0) return Result.ok();
        return Result.error(ResultCodeEnum.ADDERROR);
    }

    @PostMapping("/updateStudySrc")
    public Result updateStudySrc(@RequestBody StudySrc studySrc) {
        int i = studySrcService.updateStudySrc(studySrc);
        if(i>0) return Result.ok();
        return Result.error(ResultCodeEnum.MODIFYERROR);
    }
}

